//
//  DebugFrontBaseWindow.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/14.
//

#import "DebugFrontBaseWindow.h"

@interface DebugFrontBaseWindow ()
///
@property(nonatomic,assign,readwrite) BOOL isFirst;
///
@property(nonatomic,copy) NSString *windowId;

@end

@implementation DebugFrontBaseWindow


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        self.hidden = YES;
        #if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
            if (@available(iOS 13.0, *)) {
                UIScene *scene = [[UIApplication sharedApplication].connectedScenes anyObject];
                if (scene) {
                    self.windowScene = (UIWindowScene *)scene;
                }
            }
        #endif
        self.windowId = [NSString stringWithFormat:@"%p",self];
    }
    return self;
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    // 默认曲线图不拦截触摸事件，只有在关闭按钮z之类才响应
    if (CGRectContainsPoint(self.vc.closeButton.frame, point)) {
        return [super pointInside:point withEvent:event];
    }else if (CGRectContainsPoint(self.vc.funcButton.frame, point))
    {
        return [super pointInside:point withEvent:event];
    }
    return NO;
}

- (void)show
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:@"dissMissFount" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dissMissFount" object:@{@"title":self.windowId}];
    self.hidden = NO;
}

- (void)dissMiss
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"dissMissFount" object:nil];
    if(self.rootViewController.presentedViewController)
    {
        [self.rootViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    };
    self.rootViewController = nil;
    self.hidden = YES;
}

- (void)getNotification:(NSNotification *)notification
{
    NSString *title = notification.object[@"title"]?:@"";
    if (![title isEqualToString:self.windowId]) {
        [self dissMiss];
    }
}
@end
